{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch as t\n",
    "import sys\n",
    "sys.path.append('../')\n",
    "from utils import get_score\n",
    "import glob\n",
    "import torch as t \n",
    "import numpy as np\n",
    "import json\n",
    "import time\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "label_path =   '/home/a/code/pytorch/zhihu/ddd/labels.json'\n",
    "test_data_path='/home/a/code/pytorch/zhihu/ddd/val.npz' \n",
    "index2qid = np.load(test_data_path)['index2qid'].item()\n",
    "with open(label_path) as f: \n",
    "      labels_info = json.load(f)\n",
    "qid2label = labels_info['d']\n",
    "label2qid = labels_info['id2label']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "../checkpoints/result/LSTMText0.4119_word_val.pth\n",
      "../checkpoints/result/RCNN_0.4037_char_val.pth\n",
      "../checkpoints/result/LSTMText0.4031_char_val.pth\n",
      "../checkpoints/result/RCNN_0.4115_word_val.pth\n",
      "../checkpoints/result/DeepText0.4103_word_val.pth\n",
      "../checkpoints/result/FastText3_word_0.409160_val.pth\n",
      "../checkpoints/result/CNNText_tmp0.4109_word_val.pth\n",
      "../checkpoints/result/CNNText_tmp0.4024_char_val.pth\n"
     ]
    }
   ],
   "source": [
    "# 不进行数据增强的模型的融合\n",
    "files = glob.glob('../checkpoints/result/*val.pth')\n",
    "r2=0\n",
    "for file in files:\n",
    "    if 'MultiModel' not in file:\n",
    "        print file\n",
    "        r2+=t.load(file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "true_labels = [qid2label[index2qid[2999967-200000+ii]] for ii in range(len(r2))]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "../checkpoints/result/tmp/DeepText_word_0.410051_aug_val.pth\n",
      "../checkpoints/result/tmp/RCNN0.41344_aug_word_val.pth\n",
      "../checkpoints/result/tmp/FastText3_word_weight1_0.40853_val.pth\n",
      "../checkpoints/result/tmp/inception0.41254_aug_word_val.pth\n",
      "../checkpoints/result/tmp/MultiCNNTextBNDeep_aug_char_0.38738_val.pth\n",
      "../checkpoints/result/tmp/RCNN0.39854_aug_char_val.pth\n",
      "../checkpoints/result/tmp/LSTMText0.41368_aug_word_val.pth\n"
     ]
    }
   ],
   "source": [
    "# 进行数据增强的模型的融合\n",
    "files = glob.glob('../checkpoints/result/tmp/*val.pth')\n",
    "r_aug=0\n",
    "for file in files:\n",
    "    if 'MultiModel' not in file and 'weight5' not in file:\n",
    "        print file\n",
    "        r_aug+=t.load(file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "../checkpoints/result/tmp/MultiModelallfast_0.419088_aug_val.pth\n",
      "../checkpoints/result/tmp/MultiModel_0.4171859_val.pth\n",
      "../checkpoints/result/tmp/MultiModel2_0.42560_val.pth\n",
      "../checkpoints/result/tmp/MultiModel_0.42169_val.pth\n",
      "../checkpoints/result/tmp/MultiModelall_0.4235_aug_val.pth\n",
      "../checkpoints/result/tmp/MultiModel2w2c_0.4213_val.pth\n",
      "../checkpoints/result/tmp/MultiModelallfast_0.41652_aug_val.pth\n",
      "../checkpoints/result/tmp/MultiModel_0.41987_word_val.pth\n"
     ]
    }
   ],
   "source": [
    "# MultiModel模型的融合\n",
    "files = glob.glob('../checkpoints/result/tmp/*val.pth')\n",
    "r_multi=0\n",
    "for file in files:\n",
    "    if 'MultiModel' in file :\n",
    "        print file\n",
    "        r_multi+=t.load(file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "../checkpoints/result/tmp/MultiCNNTextBNDeep_word_weight5_0.409409_val.pth\n",
      "../checkpoints/result/tmp/LSTMText_0.41230_weight5_val.pth\n",
      "../checkpoints/result/tmp/RCNN_0.41226_weight5_val.pth\n",
      "../checkpoints/result/tmp/inception_0.409912_weight5_val.pth\n",
      "../checkpoints/result/tmp/FastText3_word_weight5_0.407992_val.pth\n"
     ]
    }
   ],
   "source": [
    "# weight=5的模型,其实没什么用\n",
    "files = glob.glob('../checkpoints/result/tmp/*val.pth')\n",
    "r_5=0\n",
    "for file in files:\n",
    "    if 'weight5' in file :\n",
    "        print file\n",
    "        r_5+=t.load(file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 上述4大类模型的融合,这权重是手动调的\n",
    "tmp = (r2+r_aug*1.15+r_multi*13+r_5*0.15)\n",
    "result=(tmp).topk(5,1)[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 手动删除一些不太重要的模型\n",
    "# tmp = tmp-t.load('../checkpoints/result/DeepText0.4103_word_val.pth')\n",
    "# tmp = tmp+t.load('../checkpoints/result/DeepText0.4103_word_val.pth')*0.2\n",
    "# tmp = tmp-t.load('../checkpoints/result/tmp/LSTMText0.41368_aug_word_val.pth')*0.1\n",
    "# tmp = tmp-t.load('../checkpoints/result/CNNText_tmp0.4024_char_val.pth')*0.05\n",
    "# tmp = tmp-t.load('../checkpoints/result/tmp/MultiCNNTextBNDeep_word_weight5_0.409409_val.pth')*0.009\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(0.4306354273126943, [118573, 66909, 43404, 30157, 22047])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算在验证集上的分数 0.43065  提交后在0.43565以上\n",
    "predict_label_and_marked_label_list = [[_1,_2] for _1,_2 in zip(result,true_labels)]\n",
    "score,_,_,ss = get_score(predict_label_and_marked_label_list)\n",
    "score,ss"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
